/**
 * 
 * 起迪科技 Copyright (c) 2014-2017 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.qidisoft.core.common.context.ApplicationContextConfigUtil;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: DownLoadFileServlet.java, v 0.1 2018年10月9日 下午6:45:09 hhl Exp $
 */
public class DownLoadFileServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private static String     upload           = null;

    static {
        upload = ApplicationContextConfigUtil.getString("file.path", null);
    }

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownLoadFileServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     * @see javax.servlet.GenericServlet#destroy()
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getParameter("filename");
        String path = request.getParameter("filepath");
        // String rootPath = ResourceBundle.getBundle("ApplicationResources")
        // .getString("protocolPath");

        String rootPath = upload;
        if (filename != null && filename != "" && path != null && path != "") {
            String path1 = rootPath + "/" + path.replace("\\", "/");
            File f = new File(path1);
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
            byte[] buf = new byte[1024];
            int len = 0;
            response.reset();
            if (filename.toLowerCase().endsWith(".txt")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("text;charset=GBK");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".doc")
                       || filename.toLowerCase().endsWith(".docx")
                       || filename.toLowerCase().endsWith(".dot")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/msword;charset=GBK");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".xls")
                       || filename.toLowerCase().endsWith(".xlsx")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/ms-excel;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".pdf")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/pdf;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".ppt")
                       || filename.toLowerCase().endsWith(".pptx")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/vnd.ms-powerpoint;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".wav")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("audio/x-wav;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".zip")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/zip;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".dll")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/x-msdownload;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".xml")
                       || filename.toLowerCase().endsWith(".xq")
                       || filename.toLowerCase().endsWith(".xql")
                       || filename.toLowerCase().endsWith(".xquery")
                       || filename.toLowerCase().endsWith(".xsd")
                       || filename.toLowerCase().endsWith(".xsl")
                       || filename.toLowerCase().endsWith(".xslt")
                       || filename.toLowerCase().endsWith(".wsdl")
                       || filename.toLowerCase().endsWith(".tld")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("text/xml;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".swf")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/x-shockwave-flash;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            } else if (filename.toLowerCase().endsWith(".vsd")
                       || filename.toLowerCase().endsWith(".vss")
                       || filename.toLowerCase().endsWith(".vss")
                       || filename.toLowerCase().endsWith(".vst")
                       || filename.toLowerCase().endsWith(".vsw")
                       || filename.toLowerCase().endsWith(".vsx")
                       || filename.toLowerCase().endsWith(".vtx")) {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/vnd.visio;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);

            } else {
                filename = new String(filename.getBytes("GBK"), "ISO8859_1");
                response.setContentType("application/x-msdownload");
                response.setHeader("Content-Disposition", "attachment; filename=" + filename);
            }
            OutputStream out = response.getOutputStream();
            while ((len = br.read(buf)) > 0)
                out.write(buf, 0, len);
            br.close();
            out.close();
        }
    }

    /**
     * 
     * @see javax.servlet.GenericServlet#init()
     */
    public void init() throws ServletException {
        // Put your code here
    }

}
